﻿using DataEditor.DataLayer;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace DataEditor.ViewModels
{
    public class PrefixViewModel : BaseViewModel
    {
        #region fields
        ObservableCollection<PrefixItemViewModel> _prefixItems;
        PrefixItemViewModel _selectedPrefixItem;
        RelayCommand _addNewCommand;
        RelayCommand _removeCommand;
        #endregion

        #region properties
        public PrefixItemViewModel SelectedPrefixItem
        {
            get { return _selectedPrefixItem; }
            set 
            { 
                _selectedPrefixItem = value;
                NotifyOfPropertyChange(() => SelectedPrefixItem);
            }
        }

        public ObservableCollection<PrefixItemViewModel> PrefixItems
        {
            get 
            {
                _prefixItems = new ObservableCollection<PrefixItemViewModel>();
                var query = from p in dataContext.UNIT_PREFIX
                            select p;

                foreach (var item in query)
                {
                    _prefixItems.Add(new PrefixItemViewModel(item));
                }

                return _prefixItems; 
            }
            set 
            { 
                _prefixItems = value;
                NotifyOfPropertyChange(() => PrefixItems);
            }
        }

        public RelayCommand AddNewCommand
        {
            get { return _addNewCommand; }
            set 
            { 
                _addNewCommand = value;
                NotifyOfPropertyChange(() => AddNewCommand);
            }
        }

        public RelayCommand RemoveCommand
        {
            get { return _removeCommand; }
            set 
            { 
                _removeCommand = value;
                NotifyOfPropertyChange(() => RemoveCommand);
            }
        }
        #endregion

        #region constructors
        public PrefixViewModel()
        {
            MakeCommands();
        }
        #endregion

        #region Action/Methods
        private void MakeCommands()
        {
            _addNewCommand = new RelayCommand(
                (exe) =>
                {
                    UNIT_PREFIX up = new UNIT_PREFIX()
                    {
                        PREFIX=string.Empty
                    };
                    dataContext.UNIT_PREFIX.AddObject(up);
                    dataContext.SaveChanges();
                    PrefixItems.Add(new PrefixItemViewModel(up));
                    NotifyOfPropertyChange(() => PrefixItems);
                },
                (canExe) =>
                {
                    return true;
                }
            );

            _removeCommand = new RelayCommand
            (
                (exe) =>
                {
                    dataContext.UNIT_PREFIX.DeleteObject(SelectedPrefixItem.Model);
                    dataContext.SaveChanges();
                    PrefixItems.Remove(SelectedPrefixItem);
                    NotifyOfPropertyChange(() => PrefixItems);
                },
                (canExe) =>
                {
                    return SelectedPrefixItem != null;
                }
            );
        }

        public void Save()
        {
            dataContext.SaveChanges();
        }
        #endregion
    }
}
